/**
 * Created by Zhouxiumin on 2017/3/28.
 */
import {BaseParser} from './BaseParser'
import * as cheerio from "cheerio";


export class TwoMangaParser extends BaseParser {
  readonly baseUrl: string = 'http://www.2manhua.com';
  readonly name: string = '爱漫画';
  readonly intro: string = '看漫画，就上爱漫画网';
  readonly expr:string = '\.2manhua\.com';
  readonly tags: Array<{title: string, url: string}> = [
    {title:'日本',url:'http://www.2manhua.com/list/area-%E6%97%A5%E6%9C%AC-order-hits-p-@page'},
    {title:'港台',url:'http://www.2manhua.com/list/area-%E6%B8%AF%E5%8F%B0-order-hits-p-@page'},
    {title:'欧美',url:'http://www.2manhua.com/list/area-%E6%AC%A7%E7%BE%8E-order-hits-p-@page'},
    {title:'韩国',url:'http://www.2manhua.com/list/area-%E9%9F%A9%E5%9B%BD-order-hits-p-@page'},
    {title:'国产',url:'http://www.2manhua.com/list/area-%E5%9B%BD%E4%BA%A7-order-hits-p-@page'},
    {title:'其他',url:'http://www.2manhua.com/list/area-%E5%85%B6%E5%AE%83-order-hits-p-@page'},
    {title:"热血",url:"http://www.2manhua.com/list/smid-1-order-hits-p-@page" },
    {title:"武侠",url:"http://www.2manhua.com/list/smid-2-order-hits-p-@page" },
    {title:"搞笑",url:"http://www.2manhua.com/list/smid-3-order-hits-p-@page" },
    {title:"耽美",url:"http://www.2manhua.com/list/smid-4-order-hits-p-@page" },
    {title:"爱情",url:"http://www.2manhua.com/list/smid-5-order-hits-p-@page" },
    {title:"科幻",url:"http://www.2manhua.com/list/smid-6-order-hits-p-@page" },
    {title:"魔法",url:"http://www.2manhua.com/list/smid-7-order-hits-p-@page" },
    {title:"神魔",url:"http://www.2manhua.com/list/smid-8-order-hits-p-@page" },
    {title:"竞技",url:"http://www.2manhua.com/list/smid-9-order-hits-p-@page" },
    {title:"格斗",url:"http://www.2manhua.com/list/smid-10-order-hits-p-@page" },
    {title:"机战",url:"http://www.2manhua.com/list/smid-11-order-hits-p-@page" },
    {title:"体育",url:"http://www.2manhua.com/list/smid-12-order-hits-p-@page" },
    {title:"运动",url:"http://www.2manhua.com/list/smid-13-order-hits-p-@page" },
    {title:"校园",url:"http://www.2manhua.com/list/smid-14-order-hits-p-@page" },
    {title:"励志",url:"http://www.2manhua.com/list/smid-15-order-hits-p-@page" },
    {title:"历史",url:"http://www.2manhua.com/list/smid-16-order-hits-p-@page" },
    {title:"伪娘",url:"http://www.2manhua.com/list/smid-17-order-hits-p-@page" },
    {title:"百合",url:"http://www.2manhua.com/list/smid-18-order-hits-p-@page" },
    {title:"后宫",url:"http://www.2manhua.com/list/smid-19-order-hits-p-@page" },
    {title:"治愈",url:"http://www.2manhua.com/list/smid-20-order-hits-p-@page" },
    {title:"美食",url:"http://www.2manhua.com/list/smid-21-order-hits-p-@page" },
    {title:"推理",url:"http://www.2manhua.com/list/smid-22-order-hits-p-@page" },
    {title:"悬疑",url:"http://www.2manhua.com/list/smid-23-order-hits-p-@page" },
    {title:"恐怖",url:"http://www.2manhua.com/list/smid-24-order-hits-p-@page" },
    {title:"职场",url:"http://www.2manhua.com/list/smid-25-order-hits-p-@page" },
    {title:"剧情",url:"http://www.2manhua.com/list/smid-27-order-hits-p-@page" },
    {title:"生活",url:"http://www.2manhua.com/list/smid-28-order-hits-p-@page" },
    {title:"幻想",url:"http://www.2manhua.com/list/smid-29-order-hits-p-@page" },
    {title:"战争",url:"http://www.2manhua.com/list/smid-30-order-hits-p-@page" },
    {title:"仙侠",url:"http://www.2manhua.com/list/smid-33-order-hits-p-@page" },
    {title:"古风",url:"http://www.2manhua.com/list/smid-36-order-hits-p-@page" },
    {title:"玄幻",url:"http://www.2manhua.com/list/smid-37-order-hits-p-@page" },
    {title:"穿越",url:"http://www.2manhua.com/list/smid-38-order-hits-p-@page" },
    {title:"卖肉",url:"http://www.2manhua.com/list/smid-39-order-hits-p-@page" },
    {title:"性转换",url:"http://www.2manhua.com/list/smid-40-order-hits-p-@page" },
    {title:"冒险",url:"http://www.2manhua.com/list/smid-41-order-hits-p-@page" },
    {title:"其他",url:"http://www.2manhua.com/list/smid-32-order-hits-p-@page" },
  ];
  readonly searchUrl = 'http://www.2manhua.com/search/q_@key';


  constructor() {
    super()
  }

  hots_parse(url, html) {
    let $ = cheerio.load(html);
    let list = [];
    let that = this;
    $(".pic img").each(function () {
      let img = $(this);

      let bm = {
        name: img.attr('alt'),
        url: that.urla(img.parent().attr('href')),
        logo: img.attr('src'),
      };
      list.push(bm);
    });
    return JSON.stringify(list);
  }

  tags_parse(url, html) {
    let $ = cheerio.load(html);
    let list = [];
    let that = this;
    $('#contList li').each(function () {
      let slf = $(this);
      let img = slf.find('img');
      let bm = {
        name:img.parent().attr('title'),
        url : that.urla(img.parent().attr('href')),
        logo: that.urla(img.attr('data-src'))
      };
      list.push(bm);
    });
    return JSON.stringify(list);
  }


  book_parse(url, html) {
    let $ = cheerio.load(html);
    let that = this;
    let data={
      name : $('.book-title h1').text().trim(),
      author : $('.detail-list.cf li').eq(2).text().trim(),
      logo : this.urla($('.book-cover').find('img').attr('src')),
      intro : $('#intro-cut').text(),
      updateTime : "",
      sections : [],
    };

    $('.chapter-list ul').each(function(){
      let al = $(this);
      let array = [];

      al.find('li a').each(function () {
        let bl = $(this);
        if(bl.attr('href')){
          let sm = {url:that.urla(bl.attr('href')),name:bl.text()};
          array.unshift(sm);
        }
      });
      array.forEach(v => data.sections.push(v));
    });

    // $('.chapter-list li a').each(function(){
    //   let al = $(this);
    //   if(al.attr('href')){
    //     let sm = {url:that.urla(al.attr('href')),name:al.text()};
    //     data.sections.push(sm);
    //   }
    // });
    return JSON.stringify(data);
  }

  section_parse(url, html) {
    let mreg = new RegExp("eval.*");
    let res = mreg.exec(html);
    let cInfo: { fs: Array<any> };
    if(res.length>0){
      let eval_str = res[0];
      console.log("res0");
      // let eval_str_test = String.raw`var test='init';eval(function(){console.log('inside eval');return 'test=\'eval test\''}());console.log('test: '+test)`;
      let eval_str_test = "";
      eval_str=eval_str.replace('return p','eval_str_test=p;return p');
      eval(eval_str);
      let eval_str_var = eval_str_test.replace('var','');
      eval(eval_str_var);
    }
    if(typeof cInfo == "undefined"){
      cInfo = {fs:[]};
    }
    console.log(cInfo);
    for (let i = 0; i < cInfo.fs.length; i++) {
      cInfo.fs[i] = "http://tupianku.333dm.com" + cInfo.fs[i];
    }
    return JSON.stringify(cInfo.fs);
  }

  public search_parse(url, html) {
    let $ = cheerio.load(html);
    let list = [];
    let that = this;
    $('.book-result li').each(function () {
      let slf = $(this);
      let img = slf.find('img');
      let bm = {
        name:img.parent().attr('title'),
        url : that.urla(img.parent().attr('href')),
        logo: that.urla(img.attr('src'))
      };
      list.push(bm);
    });
    return JSON.stringify(list);
  }

}
